
var isobtain = 'false';
var prizeNum = '';
var page=0;
var zhongjiangId = '';
$(function () {
    doAjax();
})

function doAjax() {
    page++;
    $.ajax({
        url:'/zongJiangRecordC/getZongJiangRecord',
        dataType:'json',
        data:{
            token:getItem('token'),
            isobtain:isobtain,
            page:page,
        },
        type:'post',
        beforeSend:function(){
            if(page==1){
                $('<div class="loading"><img src="../phoneImg/jiazaizhong.gif"/></div>').appendTo($('.record'));
            }
        },
        success:function (data) {
            if(data.code==1){
                prizeNum = data.datas.length;
                if(page==1){
                    $('.record').empty();
                    if(prizeNum==0){
                        mui('#refreshContainer').pullRefresh().disablePullupToRefresh();
                        $('<div class="kong"><img src="../img/kong.png" alt="暂无数据"></div>').appendTo($('.record'));
                    }
                }

                var prizeData = data.datas;
                for(var i=0;i<prizeData.length;i++){
                    var getState = ''
                    var ifGet = '';
                    var ifGetHtml = '';
                    if(isobtain=='true'){
                        //已领取
                        if(prizeData[i].jiangPinLevelValue==7||prizeData[i].jiangPinLevelValue==8){
                            ifGet = '<div class="code_box mui-clearfix">' +
                                '<span class="code">中奖编码：'+prizeData[i].code+'</span>' +
                                '<span class="copy">复制</span><input type="text" value="'+prizeData[i].code+'"></div>';
                        }else{
                            ifGet = '<div class="code_box mui-clearfix">' +
                                '<span class="code">物流单号：'+prizeData[i].logisticsCode+'</span>' +
                                '<span class="copy" code="'+prizeData[i].logisticsCode+'">复制</span><input type="text" value="'+prizeData[i].logisticsCode+'"></div>';
                        }

                        ifGetHtml = '领取时间：'+prizeData[i].obtainTime+''
                        $('<div class="goods_box mui-clearfix">\n' +
                            '<div class="lottery_img">\n' +
                            '<img src="'+prizeData[i].img+'" alt="">\n' +
                            '</div>\n' +
                            '<div class="lottery_info">\n' +
                            '<div class="name">'+prizeData[i].jiangPinName+'</div>\n' +
                            '<div class="code">中奖编码：'+prizeData[i].code+'</div>\n' +
                            '<div class="time">'+ifGetHtml+'</div>\n' +
                            '</div>\n' +
                            '<div class="lottery_grade">\n' +
                            '<img src="../img/lottry_prize'+prizeData[i].jiangPinLevelValue+'.png" alt="">\n' +
                            '</div>\n' +
                            '</div>\n' + ifGet +
                            '<div class="line"></div>').appendTo($('.record'))
                    }else{
                        ifGetHtml = '中奖时间：'+prizeData[i].createTime+''
                        if(prizeData[i].obtainStateValue==2){
                            //领取中
                            ifGet = '<div class="code_box mui-clearfix">' +
                                '<span class="code">奖品编码：'+prizeData[i].jiangPinCode+'</span>' +
                                '<span class="get">领取中</span></div>'

                            $('<div class="goods_box mui-clearfix">\n' +
                                '<div class="lottery_img">\n' +
                                '<img src="'+prizeData[i].img+'" alt="">\n' +
                                '</div>\n' +
                                '<div class="lottery_info">\n' +
                                '<div class="name">'+prizeData[i].jiangPinName+'</div>\n' +
                                '<div class="code">中奖编码：'+prizeData[i].code+'</div>\n' +
                                '<div class="time">'+ifGetHtml+'</div>\n' +
                                '</div>\n' +
                                '<div class="lottery_grade">\n' +
                                '<img src="../img/lottry_prize'+prizeData[i].jiangPinLevelValue+'.png" alt="">\n' +
                                '</div>\n' +
                                '</div>\n' + ifGet +
                                '<div class="line"></div>').appendTo($('.record'))
                        }else if(prizeData[i].obtainStateValue==1){
                            //未领取
                            ifGet = '<div class="code_box mui-clearfix">' +
                                '<span class="code">奖品编码：'+prizeData[i].jiangPinCode+'</span>' +
                                '<span class="get get2" getId="'+prizeData[i].id+'">立即领取</span></div>'

                            $('<div class="goods_box mui-clearfix">\n' +
                                '<div class="lottery_img">\n' +
                                '<img src="'+prizeData[i].img+'" alt="">\n' +
                                '</div>\n' +
                                '<div class="lottery_info">\n' +
                                '<div class="name">'+prizeData[i].jiangPinName+'</div>\n' +
                                '<div class="code">中奖编码：'+prizeData[i].code+'</div>\n' +
                                '<div class="time">'+ifGetHtml+'</div>\n' +
                                '</div>\n' +
                                '<div class="lottery_grade">\n' +
                                '<img src="../img/lottry_prize'+prizeData[i].jiangPinLevelValue+'.png" alt="">\n' +
                                '</div>\n' +
                                '</div>\n' + ifGet +
                                '<div class="line"></div>').appendTo($('.record'))
                        }

                    }
                }
            }

        }
    })
    return prizeNum;
}

mui('body').on('tap','.state_box li',function () {
    $(this).addClass('state_act');
    $(this).append('<span></span>');
    $(this).siblings().removeClass('state_act');
    $(this).siblings().find('span').hide();
    isobtain = $(this).attr('isobtain');
    mui('#refreshContainer').pullRefresh().scrollTo(0, 0);//滚动到顶部
    mui('#refreshContainer').pullRefresh().refresh(true);
    if($('.mui-pull-caption').html()=='上拉显示更多'){
        $('.mui-pull-caption').html('');
    }
    $('.record').empty();
    page=0;
    doAjax();
})

//上拉加载
mui.init({
    pullRefresh : {
        container:"#refreshContainer",//待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
        up : {
            contentrefresh : "正在加载...",//可选，正在加载状态时，上拉加载控件上显示的标题内容
            contentnomore:'我是有底线的',//可选，请求完毕若没有更多数据时显示的提醒内容；
            callback :function(){
                setTimeout(function () {
                    var numResult = doAjax();
                    if (numResult < 20) {
                        mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
                    } else {
                        mui('#refreshContainer').pullRefresh().endPullupToRefresh(false);
                    }
                }, 1000);
            } //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
        }
    }
});

//领取奖品
mui('body').on('tap','.get2',function () {
    zhongjiangId  = $(this).attr('getId');
    doprovince();
    $('.get_info').show();
})
//选择省
function doprovince() {
    $.ajax({
        url:'/areaC/province.json',
        dataType:'json',
        data:{

        },
        type:'post',
        success:function(data){
            $('<option value="">请选择省</option>').appendTo($('#province'));
            for(var i=0;i<data.datas.length;i++){

                $('<option class="provinceOption" value="'+data.datas[i].id+'">'+data.datas[i].provinceName+'</option>').appendTo($('#province'))

            }
            //查询省点击
            $('#province').change(function(){
                var optionId = $(this).val();
                doCity(optionId);
            })
        }
    })
}

function doCity(optionId){
    $.ajax({
        url:'/areaC/getCity.json',
        dataType:'json',
        data:{
            provinceId:optionId,
        },
        type:'post',
        success:function(data){
            $('#city').empty();
            $('<option value="">请选择市</option>').appendTo($('#city'));
            for(var i=0;i<data.datas.length;i++){
                $('<option class="cityOption" value="'+data.datas[i].id+'">'+data.datas[i].cityName+'</option>').appendTo($('#city'))
            }
            //市点击
            $('#city').change(function(){
                var optionId2 = $(this).val();
                doCounty(optionId2);
            })
        }
    })
}

function doCounty(optionId2){
    $.ajax({
        url:'/crm/areaC/getArea.json',
        dataType:'json',
        data:{
            cityId:optionId2,
        },
        type:'post',
        success:function(data){
            $('#county').empty();
            $('<option value="">请选择县/区</option>').appendTo($('#county'));
            for(var i=0;i<data.datas.length;i++){
                $('<option class="countyOption" value="'+data.datas[i].id+'">'+data.datas[i].areaName+'</option>').appendTo($('#county'))
            }
        }
    })
}

//提交信息
$('.submit_info').click(function () {
    var location = $('#province').find("option:selected").text()+$('#city').find("option:selected").text()+$('#county').find("option:selected").text()+$('.detail_address').val()
    $.ajax({
        url:'/zongJiangRecordC/userGetJiangPin',
        dataType:'json',
        data:{
            token:getItem('token'),
            location:location,
            id:zhongjiangId,
        },
        type:'post',
        success:function(data){
            if(data.code==1){
                mui.alert('提交信息成功，我们将在半个小时之内联系您',function () {
                    $('.get_info').hide();
                    $('.record').empty();
                    page=0;
                    doAjax();
                });
            }
            if(data.code==0){
                mui.toast(data.msg);
            }
            if(data.code==-1){
                window.location.href="login.html"
            }
        }
    })
})

//关闭
$('.close_goods').click(function () {
    $('.get_info').hide();
})

//复制
mui('body').on('tap','.copy',function () {
    //var Url2=document.getElementById("biao1");
    var Url2 = $(this).parent().find('input');
    Url2.select(); // 选择对象
    document.execCommand("Copy"); // 执行浏览器复制命令
    mui.toast('已复制')
})